package com.study.shop.service.impl;

import com.study.shop.common.ShopException;
import com.study.shop.entity.dto.AttrDTO;
import com.study.shop.entity.dto.AttrValueDTO;
import com.study.shop.entity.po.Attr;
import com.study.shop.entity.po.AttrValue;
import com.study.shop.entity.po.Value;
import com.study.shop.mapper.AttrMapper;
import com.study.shop.service.AttrService;
import com.study.shop.service.ValueService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AttrServiceImpl implements AttrService {
    private final AttrMapper attrMapper;
    private final ValueService valueService;

    @Override
    public List<AttrValueDTO> getAttrByCategory(int thirdLevel) {
        return attrMapper.getAttrByCategory(thirdLevel);
    }

    @Override
    @Transactional
    public boolean saveAttrInfo(AttrDTO attrDTO) {
        String attrName = attrDTO.getAttrName();
        if (!StringUtils.hasText(attrName)) {
            throw new ShopException("请传入属性值");
        }
        // 保存属性名
        Attr attr = new Attr();
        attr.setName(attrName);
        // 确保没有重名
        int count = attrMapper.existName(attrName);
        if (count > 0) {
            throw new ShopException("属性名已经存在");
        }
        // 1. 插入属性名数据
        int insert = attrMapper.insert(attr);
        // 2. 插入属性值
        List<Value> values = valueService.saveValueList(attrDTO.getValueList());
        // 3. 建立属性和分类之间的关系
        attrMapper.categoryAttrRelationship(attrDTO.getThirdLevel(), attr.getId());
        // 4. 属性和属性值之间建立联系
        AttrValue attrValue = new AttrValue(attr, values);
        attrMapper.attrValueRelationship(attrValue);
        return insert > 0;
    }

    @Override
    @Transactional
    public boolean updateAttrInfo(AttrDTO attrDTO) {
        if (ObjectUtils.isEmpty(attrDTO)) {
            throw new ShopException("请传入属性信息");
        }
        // 1. 更新属性名
        Integer attrId = attrDTO.getId();
        Attr attr = attrMapper.selectByPrimaryKey(attrId.longValue());
        if (!attr.getName().equals(attrDTO.getAttrName())) {
            Attr a = new Attr();
            a.setName(attrDTO.getAttrName());
            a.setId(attrId);
            attrMapper.updateByPrimaryKeySelective(a);
        }
        // 2. 更新属性值
        // 2.1 删除属性对应的所有属性值
        deleteValuesByAttrId(attrId);
        // 2.2 新增对应的所有属性值、并建立对应关系
        List<Value> values = valueService.saveValueList(attrDTO.getValueList());
        AttrValue attrValue = new AttrValue(attr, values);
        attrMapper.attrValueRelationship(attrValue);
        return true;
    }

    @Override
    @Transactional
    public boolean deleteAttrInfo(Integer id) {
        deleteValuesByAttrId(id);
        attrMapper.deleteByPrimaryKey(id.longValue());
        return true;
    }

    /**
     * 根据attrId删除属性值
     *
     * @param id attrId
     */
    private void deleteValuesByAttrId(Integer id) {
        List<Integer> vIds = attrMapper.getValueIdsByAttrId(id);
        attrMapper.deleteAttrValueRelationshipByAttrId(id);
        valueService.deleteValuesByIds(vIds);
    }
}
